﻿using System;
using System.Collections.Generic;
using System.Text;
using NBearLite;
using DataAccess;
using System.Data;
using CommonUtility.Utility;

namespace UIDevCore
{
    public enum EConditionOperate
    {
        Equal,
        NotEqual,
        Contains,
        Great,
        GreatEqual,
        Less,
        LessEqual,
        In,
        NotIn,
        Like,
        Between,
    }

    public class UISearchHelper : SearchHelper
    {
        #region 构造器
        public UISearchHelper(IQueryTable table)
            : base(table)
        {
        }

        public UISearchHelper(IQueryTable table, string postData)
            : base(table)
        {
            this.ListSetting = UIHelper.GetListSetting(postData);
        }

        public UISearchHelper(IQueryTable table,EDatabase db)
            : base(table, db)
        {
        }
        public UISearchHelper(IQueryTable table, string postData, EDatabase db)
            : base(table, db)
        {
            this.ListSetting = UIHelper.GetListSetting(postData);
        }
        #endregion

        #region Props
        public PostListSetting ListSetting;

        public bool HasCondition { get { return this.ListSetting != null && this.ListSetting.HasCondition; } }
        #endregion

        #region Search
        public UISearchHelper SetCondition(string condition)
        {
            this.ListSetting = new PostListSetting();
            this.ListSetting.Where = condition;
            return this;
        }

        /// <summary>
        /// Where语句
        /// </summary>
        /// <param name="where"></param>
        /// <returns>WhereClip</returns>
        public UISearchHelper Where(QueryColumn column)
        {
            return this.Where(column, EConditionOperate.Equal);
        }
        /// <summary>
        /// Where语句
        /// </summary>
        /// <param name="where"></param>
        /// <returns>WhereClip</returns>
        public UISearchHelper Where(QueryColumn column, EConditionOperate operate)
        {
            WhereClip whereClip = this.GetWhere(column, operate);
            if (!WhereClip.IsNullOrEmpty(whereClip))
                this.Where(whereClip);
 
            return this;
        }

        public ResponseListData GetResponseListData()
        {
            if (!string.IsNullOrEmpty(this.ListSetting.Sort))
                this.AddOrderByClip(this.ListSetting.Sort);

            int nTotal = 0;
            DataTable dt = this.GetData(this.ListSetting.PageSize, this.ListSetting.PageIndex, ref nTotal);
            return new ResponseListData() { rows = dt == null ? string.Empty : JsonHelper.SerializeData(dt), total = nTotal };
        }

        public WhereClip GetWhere(QueryColumn column)
        {
            return this.GetWhere(column, EConditionOperate.Equal);
        }

        public WhereClip GetWhere(QueryColumn column, EConditionOperate operate)
        {
            if (operate == EConditionOperate.Between)//范围查询特殊处理
            {
                object begin = this.GetValue(column);
                object end = this.GetValue(column.ColumnName + UIConst.Field_Range_End);
                WhereClip whereClip = null;
                if(begin != null)
                    whereClip = column >= begin;
                if (end != null)
                {
                    if (WhereClip.IsNullOrEmpty(whereClip))
                        return column <= end;
                    else
                        return whereClip && column <= end;
                }
                return whereClip;
            }
            else
            {
                object condition = this.GetValue(column);
                if (condition != null)
                {
                    switch (operate)
                    {
                        case EConditionOperate.Equal:
                            return column == condition;
                        case EConditionOperate.NotEqual:
                            return column != condition;
                        case EConditionOperate.Contains:
                            return column.Contains(condition.ToString());
                        case EConditionOperate.Great:
                            return column > condition;
                        case EConditionOperate.GreatEqual:
                            return column >= condition;
                        case EConditionOperate.Less:
                            return column < condition;
                        case EConditionOperate.LessEqual:
                            return column <= condition;
                        case EConditionOperate.In:
                            return column.In(condition.ToString().Split(','));
                        case EConditionOperate.NotIn:
                            return column.NotIn(condition);
                        case EConditionOperate.Like:
                            return column.Like(condition.ToString());
                    }
                }
            }
            return null;
        }
        #endregion

        #region GetValue
        public object GetValue(QueryColumn column)
        {
            return this.GetValue(column.ColumnName);
        }

        public object GetValue(string paramName)
        {
            return UIHelper.getDicValue(paramName, this.ListSetting.Conditions);
        }

        public D GetValue<D>(QueryColumn column, D defaultValue)
        {
            return this.GetValue<D>(column.ColumnName, defaultValue);
        }

        public D GetValue<D>(string paramName, D defaultValue)
        {
            return UIHelper.getDicValue<D>(paramName, this.ListSetting.Conditions, defaultValue);
        }
        #endregion
    }
}
